
package com.bda.mediatheque.controller;

import com.bda.mediatheque.entities.Utilisateur;
import com.bda.mediatheque.service.ImplUtilisateurDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This servlet is used for the login action
 * @author AOU
 */
@WebServlet(name="LogInServlet", urlPatterns={"/LogIn"})
public class LogInServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try {
            
            String forwardStatement = "Login.jsp";
            //Get the data from login's form
            String login  = (String) request.getParameter("login");
            String pwd   = (String) request.getParameter("pwd");

            // Find user by login and password.
            ImplUtilisateurDao utilisateurDao = new ImplUtilisateurDao();
            Utilisateur loggedUser = utilisateurDao.findUserByLogin(login);
            if (null != loggedUser)
            {
                if (pwd.equals(loggedUser.getPwd()))
                {
                    // User logged in
                    request.setAttribute("loggedUser",loggedUser.getLogin());
                    request.getSession().setAttribute("loggedUser",loggedUser.getLogin());
                    forwardStatement = "UserSpace.jsp";
                }
                else
                {
                    // Wrong password
                    request.setAttribute("Error","Votre mot de passe est erronée.");
                    forwardStatement = "Login.jsp";
                }
            }
            else
            {
                // User doesn't exist, the login page is again displayed
                forwardStatement = "Login.jsp";
            }
            request.getRequestDispatcher(forwardStatement).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistebce provide
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
